/*************************************************************************
> File Name: newWash.c
> Author: zhaopeng
> Mail: zhaopeng@miaozhen.com
> Created Time: Thu 09 Feb 2017 03:55:12 PM CST
************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include <strstream>

using namespace std;

const int BUFFER_LEN=1024;

int main(int argc, char* argv[])
{
    if(3 != argc)
    {
        printf("wrong input file path\n");
        return -1;
    }

    FILE *fpFileIn = fopen(argv[1], "r");
    if(!fpFileIn)
    {
        printf("can't open input file\n");
        return -1;
    }

    ofstream fout;

    char inbuffer[BUFFER_LEN];
    char outbuffer[BUFFER_LEN];
    memset(inbuffer, BUFFER_LEN, 0);
    memset(outbuffer, BUFFER_LEN, 0);
    set<string> lines;

    while(fgets(inbuffer, BUFFER_LEN, fpFileIn))
    {
        int index= 0;
        int outindex = 0;
        int  needNextLine = 0;
        while(index<BUFFER_LEN && inbuffer[index] && '\n'!=inbuffer[index])
        {
            if('|' == inbuffer[index])
            {
                index++;
                continue;
            }

            if(index>0 && inbuffer[index-1]=='|' && needNextLine)
            {
                outbuffer[outindex++] = '\n';
            }
            needNextLine = 1;
            if('a'<=inbuffer[index] && 'z'>=inbuffer[index])
            {
                outbuffer[outindex++] = inbuffer[index] - ('a' - 'A');
            }
            else
            {
                outbuffer[outindex++] = inbuffer[index];
            }
            index++;

        }// enf of inner while
        outbuffer[outindex] = '\0';
        istrstream sin(outbuffer);
        string line;
        while(sin>>line)
        {
            lines.insert(line);
        }
        memset(inbuffer, BUFFER_LEN, 0);
        memset(outbuffer, BUFFER_LEN, 0);
    }// end  of outer while

    fout.open(argv[2]);
    for(set<string>::iterator itor=lines.begin(); itor!=lines.end(); itor++)
    {
        fout<<*itor<<endl;
    }
    fclose(fpFileIn);
    return 0;
}

